home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 236 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help about structure
  5. Date: 3 Jan 1996 10:22:15 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4ce6v7$j6p@umbc9.umbc.edu>
  8. References: <4c3t9q$aob@chleuasme.francenet.fr>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. jo  <jobloch@micronet.fr> wrote:
  13. |> How can I translate from Pascal to C such a structure:
  14. |> token=record;
  15. |>     begin
  16. |>          nature:tokennature;
  17. |>          case nature : tokennature of
  18. |>                 tkinteger: (i:integer);
  19. |>                 tkreal: (r:double);
  20. |>                 ...
  21. |>     end;
  22. |> 
  23. |> Can i use "switch" in a structure implementation ?
  24.  
  25. This is sorta like the FAQ question 20.26:
  26.  
  27. "Are there programs for converting Pascal or FORTRAN to C?"
  28.  
  29. Since the answer is obviously yes then any portable Pascal program
  30. can be written in portable C. I've had great success using a program
  31. called p2c under UNIX, but there's probably implementations or
  32. similar programs for MS-DOS. The way p2c does a conversion like this
  33. is to have a union inside the structure. Here's an example:
  34.  
  35. typedef enum {tkinteger, tkreal} tokennature;
  36.  
  37. typedef struct
  38. {
  39.   tokennature nature;
  40.   union { int i; double r; } U;
  41. } token;
  42.  
  43. So you can't have a switch in the structure itself, but will undoubtedly
  44. be using a switch when accessing the members of the union inside of
  45. the structure.
  46. -- 
  47. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  48.  
  49. Jonas J. Schlein  (schlein@gl.umbc.edu)
  50.